package com.fourth.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.UbBusinessInfoDao;
import com.fourth.dao.UbOrderDao;
import com.fourth.dao.UbStockDao;
import com.fourth.service.UbBusinessInfoService;
import com.fourth.service.UbOrderService;
import com.fourth.service.UbStockService;
import com.ljx.entity.Order;
import com.ljx.entity.Stock;
import com.ljx.vo.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Mr.ma
 * @since 2023/3/8 21:32
 **/
@Component
public class PayUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private UbOrderService ubOrderService;

    @Resource
    private UbStockService ubStockService;

    @Resource
    private RedisLockUtil redisLockUtil;

    @Resource
    private UbBusinessInfoService ubBusinessInfoService;

    /**
     * 超时时间 10s
     */
    private static final int TIMEOUT = 10*1000;

    @Transactional(rollbackFor = Exception.class)
    public Result paySuccess(String code,int paySort){
        List<Order> orderByCode = ubOrderService.getOrderByCode(code);
        if (orderByCode.get(0).getState() == 0){
            return new Result(2000,"购买成功");
        }
        BigDecimal totalMoney = orderByCode.get(0).getTotalmoney();
        String bid = orderByCode.get(0).getBid();
        ubBusinessInfoService.changeMoney(bid,totalMoney);
        for (int i = 0; i < orderByCode.size(); i++) {
            // 修改订单状态
            orderByCode.get(i).setPaysort(paySort);
            orderByCode.get(i).setSend(0);
            orderByCode.get(i).setState(0);
            ubOrderService.updateById(orderByCode.get(i));
            ubStockService.changeStockSellNum(orderByCode.get(i).getGoodsid(),orderByCode.get(i).getNum());
        }

        // 删除缓存中的信息
        stringRedisTemplate.opsForValue().getOperations().delete("order::" + code);

        return new Result(2000,"购买成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public Result changeStock(String goodsId,Integer num){
        // 修改订单库存
        // 设置锁释放时间
        long time = System.currentTimeMillis() + TIMEOUT;
        if(!redisLockUtil.lock(goodsId,String.valueOf(time))){
            return new Result(5000,"很抱歉，服务器拥挤，换个姿势再试试~~");
        }

        // 执行业务
        Stock stock = ubStockService.selectByGoodsId(goodsId);

        Long oldResidue = Long.valueOf(stock.getResidue());
        if (oldResidue == 0 || num > oldResidue){
            return new Result(5000,"很抱歉，商品库存不足");
        }

        stock.setResidue(String.valueOf(oldResidue - Long.valueOf(num)));
        int update = ubStockService.updateStock(stock);
        if (update <= 0){
            return new Result(5000,"服务器异常，请联系管理员");
        }
        return new Result(2000,"库存修改成功");
    }

    public void addRedis(String code,String value){
        stringRedisTemplate.opsForValue().set("order::" + code, value);
    }

    public String getRedis(String code){
        return stringRedisTemplate.opsForValue().get("order::" + code);
    }
}
